﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Common;

namespace Heart.Controllers
{

    public class PublicController : Controller
    {
        #region 公用属性

        public int albumHeight = ConfigHelper.AlbumHeight();
        public int albumWidth = ConfigHelper.AlbumWidth();
        #endregion

        //
        // GET: /Public/
        [ActionName("Upload")]
        [HttpPost]
        public ActionResult Index()
        {
            string path = "";
            string filename = "";
            string strfile = "NoFile";
            string pathfile = "";
            foreach (string upload in Request.Files)
            {
                var file = Request.Files[upload];
                if (!PublicClass.HasFile(file)) continue;
                if (PublicClass.IsAllowedExtension(file))
                {
                    //Path.GetFileName(Request.Files[upload].FileName);
                    Random ra = new Random();
                    string fileStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ra.Next(1000, 9999);
                    filename = fileStr + Path.GetExtension(file.FileName);
                    string strDay = System.DateTime.Now.ToString("yyyyMM");
                    string DirUrl = Server.MapPath("~/upload/product/" + strDay + "/");
                    string SmallDirUrl = Server.MapPath("~/upload/product/small/" + strDay + "/");
                    if (!System.IO.Directory.Exists(DirUrl))      //检测文件夹是否存在，不存在则创建
                    {
                        System.IO.Directory.CreateDirectory(DirUrl);
                    } if (!System.IO.Directory.Exists(SmallDirUrl))      //检测文件夹是否存在，不存在则创建
                    {
                        System.IO.Directory.CreateDirectory(SmallDirUrl);
                    }
                    //path = AppDomain.CurrentDomain.BaseDirectory + "/upload/product/" + strDay;
                    //string smallPath = AppDomain.CurrentDomain.BaseDirectory + "/upload/product/small/" + strDay;
                    //strfile = "/upload/product/" + strDay + "/" + filename;
                    //pathfile = "product/" + strDay + "/" + filename;
                    path = AppDomain.CurrentDomain.BaseDirectory + "/upload/product/";
                    string smallPath = AppDomain.CurrentDomain.BaseDirectory + "/upload/product/small/";
                    strfile = "/upload/product/" + filename;
                    pathfile = filename;
                    string pPath = AppDomain.CurrentDomain.BaseDirectory + strfile;
                    string pSavedPath = smallPath + "\\" + filename;
                    try
                    {
                        //上传大图
                        file.SaveAs(Path.Combine(path, filename));
                        //上传缩略图
                        PublicClass.GetSmaller(pPath, pSavedPath, albumWidth, albumHeight);

                    }
                    catch (Exception ex)
                    {
                        strfile = "Error";
                    }
                }
                else
                {
                    strfile = "格式不正确！";
                }

            }
            return Content(pathfile, "text/html");
        }
        public ActionResult Delete(string id)
        {
            string msg = "Fail";
            var url = id;//Request.QueryString["str"];            
            try
            {
                System.IO.File.Delete(Server.MapPath(url));
                msg = "Success";
            }
            catch (Exception)
            {
                msg = "Error";
            }

            return Content(msg, "text/html");
        }

    }
}
